<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="query">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表  v-loading="listLoading"-->
    <el-table :data="departments" highlight-current-row  v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="编号"  width="60">
      </el-table-column>
      <el-table-column prop="deptId" label="部门编号" width="120" >
      </el-table-column>
      <el-table-column prop="dpetname" label="部门名称" width="120" >
      </el-table-column>
      <el-table-column prop="creationTime" label="创建时间" width="200" >
      </el-table-column>
      <el-table-column prop="superiorDpetname.dpetname" label="上级部门" width="100" >
      </el-table-column>
      <el-table-column prop="creator" label="创建人" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="totals"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="部门名称" prop="dpetname">
          <el-input v-model="editForm.dpetname" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" prop="superiorDept">
          <el-input v-model="editForm.superiorDept" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="editForm.creator" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="editForm">
        <el-form-item label="部门名称" prop="dpetname">
          <el-input v-model="addForm.dpetname" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" prop="superiorDept">
          <el-input v-model="addForm.superiorDept" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="creator">
          <el-input v-model="addForm.creator" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';

export default {
  data() {
    return {
      // 高级查询
      filters: {
        keyword: ''
      },
      // 列表数据
      departments: [],
      // 二级树
      deptTree:[],
      // 总条数
      totals: 0,
      // 当前页条数
      pageSize: 5,
      // 当前页
      currentPage:1,
      // 页面忙等
      listLoading: false,
      sels: [],//列表选中列

      //编辑界面是否显示
      editFormVisible: false,
      // 修改忙等
      editLoading: false,
      // 修改表单校验
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      editForm: {
        // id
        deptId:0,
        // name
        dpetname:"",
        // 时间
        creationTime:"",
        // 上级部门
        superiorDept:"",
        // 上级部门id
        superiorDeptId:"",
        // 创建时间
        creator:""
      },
      //新增界面是否显示
      addFormVisible: false,
      // 新增忙等
      addLoading: false,
      // 新增表单校验
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //新增界面数据
      addForm: {
        // id
        deptId:"",
        // name
        dpetname:"",
        // 时间
        creationTime:"",
        // 上级部门
        superiorDept:"",
        // 创建时间
        creator:""
      }

    }
  },
  methods: {
    query() {
      // 高级查询定位第一页
      this.currentPage = 1;
      this.getDepartments();
    },
    //性别显示转换
    formatSex: function (row, column) {

    },
    // 点击页数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDepartments();
    },

    //获取部门列表
    getDepartments() {
      // 携带到后端的参数
      let para = {
        pageSize: this.pageSize,
        currentPage: this.currentPage,
        keyword: this.filters.keyword
      };
      // 忙等
      this.listLoading = true;
      //NProgress.start();
      // 发送axios请求获取后端数据
      this.$http.post("/dept",para)
        .then(result=>{
          // 关闭忙等
          this.listLoading = false;
          // console.log(result);
          // 列表数据赋值
          this.departments = result.data.rows;
          // totals赋值
          this.totals = result.data.totals;
          // console.log(result.data.totals);
        })
        .catch(result=>{
          // 请求失败
          this.$message({
            message: '网络繁忙',
            type: 'error'
          });
        })
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        console.log(row)
        this.listLoading = true;
        //将id携带过去
        // console.log(row.novelId);
        this.$http.delete("/dept/"+row.deptId)
          .then(result=>{
            //关闭忙等框
            this.listLoading = false;
            //判断
              //跳到第一页，防止页面没数据
              this.currentPage = 1;
              //刷新页面
              this.getDepartments();
          })
          .catch(result=>{
            this.$message({
              type: 'error',
              message: '删除错误,我们正在抢救程序员!!'
            });
            this.currentPage = 1;
            //刷新页面
            this.getDepartments();
          })
      }).catch(() => {
        //取消就没有了，then才是判断删除与否
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.getDeptTree();
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.getDeptTree();
      this.addFormVisible = true;
      this.addForm = {
        deptId:"",
        // name
        dpetname:"",
        // 时间
        creationTime:"",
        // 上级部门
        superiorDept:"",
        // 创建时间
        creator:""
      };
    },
    //编辑
    editSubmit: function () {
      this.editLoading = true;
      let para = Object.assign({}, this.editForm);
      console.log(para)
      this.$http.post("/dept/save",para)
        .then(result=>{
          //关闭忙等框
          this.editLoading = false;
          this.editFormVisible = false;//关闭模态框
          //跳到第一页，防止页面没数据
          this.currentPage = 1;
          //刷新页面
          this.getDepartments();
        })
        .catch(result=>{
          //关闭忙等
          this.editLoading = false;
          this.$message({
            type: 'error',
            message: "新增失败，我们正在抢救程序员！！！"
          });
          //跳到第一页
          this.currentPage = 1;
          //刷新页面
          this.getDepartments();
        });
    },
    //新增
    addSubmit: function () {
      this.addLoading = true;
      let para = Object.assign({}, this.addForm);
      console.log(para)
      this.$http.post("/dept/save",para)
        .then(result=>{
          //关闭忙等框
          this.addLoading = false;
          this.addFormVisible = false;//关闭模态框
          //跳到第一页，防止页面没数据
          this.currentPage = 1;
            //刷新页面
          this.getDepartments();
        })
        .catch(result=>{
          //关闭忙等
          this.addLoading = false;
          this.$message({
            type: 'error',
            message: "新增失败，我们正在抢救程序员！！！"
          });
          //跳到第一页
          this.currentPage = 1;
          //刷新页面
          this.getDepartments();
        });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { ids: ids };
        batchRemoveDepartment(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getDepartments();
        });
      }).catch(() => {

      });
    },
    // todo 部门 二级树
    getDeptTree(){
      this.$http.post("/dept/tree")
        .then(result=>{
          console.log(result.data);
          this.deptTree = result.data;
        })
    }
  },
  mounted() {
    this.getDepartments();
  }
}

</script>

<style scoped>

</style>
